package week11;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Work3Client
 * @description: TODO
 * @date 2021/11/18 14:36
 */
public class Work3Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("192.168.31.80", 7777);

        InputStream is = socket.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        OutputStream os = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);

        File file = getFile();
        FileInputStream fis = new FileInputStream(file);

        byte[] bytes = new byte[1024];
        int len;
        while ((len = fis.read(bytes)) != -1) {
            dos.write(bytes);
        }

        fis.close();
        socket.close();
    }

    public static File getFile() {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入文件：");

        while (true) {
            String path = s.next();
            File file = new File(path);
            if (!file.exists()) {
                System.out.println("文件不存在，请重新输入：");
            } else if (file.isDirectory()) {
                System.out.println("该文件是文件夹，请重新输入：");
            } else {
                System.out.println("是文件");
                return file;
            }
        }
    }
}
